<?php

class Main extends Core {
    
    function __construct() {
        
        parent::__construct();
        
    }
    
    public function pages_tree() {
        
        parent::render($this->load->pages_tree());
    }
    
    public function menu($array) {
        
        $result = '<ul>';
        
        foreach($array['key'] as $i => $title) {
            
            if ($this->usr->check_rights('view', ($array['value'][$i] == '') ? 'www' : $array['value'][$i])) {
                
                $class = ($array['value'][$i] == substr($this->uri->uri_string(), 1)) ? ' class="active" ' : '';
                
                $result .= '<li'.$class.'>'.anchor($array['value'][$i], $title).'</li>';
            }
        }
        
        $result .= '<ul>';
        
        parent::render($result);
    }
    
    public function menu_page($page = false) {
        
        $tree = $this->load->tree();
        $page = ($page == false) ? $this->uri->current_uri() : $page;
        
        if (!isset($tree[$page])) {
            
            parent::render('');
            return;
        }
        
        $page_id = $tree[$page]['id'];
        
        $result = '<ul>'; $i = 0;
        
        foreach($tree as $row) {
            
            if ($row['parent_id'] == $page_id && $this->usr->check_rights('view', $row['url'])) {
                
                $class = ($this->uri->current_uri() == $row['uri']) ? ' class="active" ' : '';
                
                $result .= '<li'.$class.'>'.anchor($row['url'], $row['title']).'</li>';
                $i++;
            }
        }
        
        $result .= '<ul>';
        
        $result = ($i == 0) ? '' : $result;
        
        parent::render($result);
    }
}